
import 'package:dio/dio.dart';
import 'package:vega/http/dio_instance.dart';
import 'package:vega/repo/model/home_list_model.dart';
import 'package:vega/repo/model/play_detail_model.dart';
import 'package:vega/repo/model/play_source_model.dart';
import 'package:vega/repo/model/search_list_model.dart';

class Api {
  static Api? _instance;
  Api._internal();

  static Api instance() {
    return _instance ??= Api._internal();
  }

  Future<List<HomeListData>?> homeList() async {
    Response resp = await DioInstance.instance().get(path: "api/home");
    final data = resp.data['data'] as List<dynamic>;
    return data.map((e) => HomeListData.fromJson(e)).toList();
  }

  Future<List<SearchListItem>?> searchList(String kw, int p) async {
    final resp = await DioInstance.instance().get(path: "api/search/?kw=$kw&p=$p");
    final json = resp.data;
    final ret = SearchListModel.fromJson(json).data;
    return ret;
  }

  Future<PlayDetialData> playDetail(String url) async {
    final resp = await DioInstance.instance().get(path: "api/detail/?url=$url");
    final json = resp.data;
    final ret = PlayDetailModel.fromJson(json).data;
    return ret!;
  }

  Future<List<PlaySourceItem>?> playSource(String url) async {
    final resp = await DioInstance.instance().get(path: "api/source/?url=$url");
    final json = resp.data;
    final ret = PlaySourceModel.fromJson(json).data;
    return ret;
  }
}